package cn.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.password.PasswordEncoder;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.always.xiajia.admin.XiajiaAdminApplication;
import cn.always.xiajia.admin.sys.entity.SysRole;
import cn.always.xiajia.admin.sys.entity.SysUser;
import cn.always.xiajia.admin.sys.mapper.SysUserMapper;
import cn.always.xiajia.admin.sys.service.SysUserService;
import cn.always.xiajia.admin.sys.service.impl.SysRoleServiceImpl;
import cn.always.xiajia.framework.common.enums.UserTypeEnum;
import cn.always.xiajia.framework.common.mybatisplus.util.ExtFieldInjectUtils;
import cn.always.xiajia.framework.common.util.ExtLocalDateTimeUtil;
import cn.always.xiajia.framework.common.util.ExtMapUtils;
import cn.always.xiajia.framework.common.util.ExtStringUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;

@SpringBootTest(classes = { XiajiaAdminApplication.class })
public class test {

	@Resource
	private SysUserMapper userMapper;

	@Resource
	private SysUserService userService;

	@Resource
	private PasswordEncoder passwordEncoder;

	@Test
	public void passwordEncoderTest() {
		System.out.println(passwordEncoder.encode("123456"));
	}

	// @Test
	public void insert() {
		SysUser user = new SysUser();
		user.setUserCode("admin").setUserName("超级管理员").setStatus("1").setPassword("123456").setModifyTime(ExtLocalDateTimeUtil.now())
				.setCreateTime(ExtLocalDateTimeUtil.now());
		// 插入一条
		Boolean i = userService.save(user);

		System.out.println(i);

		System.out.println(user.toString());

	}

	// @Test
	public void select() {
		LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		// lambdaQueryWrapper.like(SysUser::getUserCode, "admi");
		List<SysUser> users = userService.list(lambdaQueryWrapper);
		System.out.println("6666666" + JSONUtil.toJsonStr(users));

		System.out.println(UserTypeEnum.ADMIN);
		System.out.println(UserTypeEnum.ADMIN.getName());

	}

	// @Test
	public void select2() {
		// 数据库查询字段需要下划线表示
		LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(SysUser::getUserId, 1);
		Page<SysUser> page = userService.page(new Page<>(1, 1), lambdaQueryWrapper);
		System.out.println(JSONUtil.toJsonStr(page));

		// ExtFieldInjectUtils.injectField(page.getRecords().get(0),
		// SysUser::getDeptId, SysDeptServiceImpl.class, SysUser::getDeptName);

		// ExtFieldInjectUtils.injectField(page.getRecords().get(0),
		// SysUser::getDeptId, SysDeptServiceImpl.class, SysDept::getDeptId,
		// SysUser::getDeptName);
		// ExtFieldInjectUtils.injectEntity(page.getRecords().get(0),
		// SysUser::getDeptId, SysDeptServiceImpl.class, SysUser::getDept);

		// ExtFieldInjectUtils.injectField(page, SysUser::getDeptId,
		// SysDeptServiceImpl.class, SysDept::getDeptId, SysUser::getDeptName);
		// ExtFieldInjectUtils.injectEntity(page, SysUser::getDeptId,
		// SysDeptServiceImpl.class, SysDept::getDeptId, SysUser::getDept);

		// ExtFieldInjectUtils.injectListFieldPage(page, SysUser::getUserId,
		// SysUserRoleServiceImpl.class, SysUserRole::getUserId,
		// SysUserRole::getRoleId);
		List<Long> ids = new ArrayList<>();
		ids.add(1L);
		page.getRecords().get(0).setRoleIds(ids);
		ExtFieldInjectUtils.injectEntityByList(page.getRecords(), SysUser::getRoleIds, SysRoleServiceImpl.class, SysRole::getRoleId,
				SysUser::getRoles);
		System.out.println(JSONUtil.toJsonStr(page));
		System.out.println(UserTypeEnum.ADMIN);
		System.out.println(UserTypeEnum.ADMIN.getName());

	}

	public static void main(String[] args) {
		Map<String, Object> param = new HashMap<>();
		// ( d && (d || d)) && d || d
		param.put("(EQ_userCode && EQ_userCode || EQ_userCode) || (EQ_userCode || EQ_userCode) && EQ_userCode", "1");
		if (!ExtMapUtils.isEmpty(param)) {
			for (String key : param.keySet()) {
				Object value = param.get(key);
				if (!ExtStringUtil.isBlank(key) && ObjectUtil.isNotEmpty(value)) {
					key = key.replaceAll(" ", "");
					key = key.replace("_OR_", "||").replace("_AND_", "&&");
					// key = key.replaceAll("\\|\\|", "\\|").replaceAll("&&",
					// "&");\
					key = test(key);
					System.out.println(key);
					String[] ors = key.split("\\|\\|");
					for (int i = 0; i < ors.length; i++) {
						String or = ors[i];

						String[] ands = or.split("&&");
						for (int j = 0; j < ands.length; j++) {
							String and = ands[j];
							if (and.startsWith("(") && and.endsWith(")")) {
								and = and.substring(1, and.length() - 1);
								System.out.println("-----有括号----");
								System.out.println(and);
								if (and.contains("|")) {

								} else if (and.contains("&")) {

								} else {

								}
							} else {
								System.out.println("-----无括号----");
								System.out.println(and);
							}
						}
					}

				}
			}

		}
	}

	public static String test(String key) {
		List<Integer[]> braketsList = new ArrayList();
		Integer[] brakets = new Integer[2];

		for (int i = 0; i < key.length(); ++i) {
			if (key.charAt(i) == '(') {
				if (brakets[0] != null) {
					throw new IllegalArgumentException("Does not support nesting brackets in search condition key \"" + key + "\".");
				}

				brakets[0] = i;
			} else if (key.charAt(i) == ')') {
				brakets[1] = i;
				if (brakets[0] == null) {
					throw new IllegalArgumentException("Search condition key \"" + key + "\" got the wrong brackets position.");
				}

				braketsList.add(brakets);
				brakets = new Integer[2];
			}
		}

		String temp = "temp_string_for_replace_symbol";

		for (int i = braketsList.size() - 1; i >= 0; --i) {
			Integer[] braketArray = (Integer[]) braketsList.get(i);
			String partKey = key.substring(braketArray[0] + 1, braketArray[1]);
			key = key.replace(partKey, temp);
			partKey = partKey.replaceAll("\\|\\|", "\\|").replaceAll("&&", "&");
			key = key.replace(temp, partKey);
		}

		return key;
	}

}
